package com.onlinevideo.servlet;

import com.onlinevideo.entity.Category;
import com.onlinevideo.entity.User;
import com.onlinevideo.entity.Video;
import com.onlinevideo.service.*;
import com.onlinevideo.service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UrlServlet extends BaseServlet{

    private static String basePath = "/WEB-INF/view/";

    private RoleService roleService = new RoleServiceImpl();

    private AuthorityService authorityService = new AuthorityServiceImpl();

    private VideoService videoService = new VideoServiceImpl();

    private CommentService commentService = new CommentServiceImpl();

    private CategoryService categoryService = new CategoryServiceImpl();

    private HttpSession session = null ;

    //首页网关
    protected void index(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher("index_bf.jsp").forward(req,resp);
    }

    //登录网关
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"login.jsp").forward(req,resp);
    }

    //注册网关
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"register.jsp").forward(req,resp);
    }

    //权限列表
    protected void authoritylist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("authorities",authorityService.queryAll());
        req.getRequestDispatcher(basePath+"authority_list.jsp").forward(req,resp);
    }

    //后台首页网关
    protected void bgindex(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        session = req.getSession();
        User bguser = (User) session.getAttribute("bguser");
        if(bguser == null ){
            resp.sendRedirect("../gateway?action=login");
            req.getRequestDispatcher(basePath+"login.jsp").forward(req,resp);
        }else{
            req.getRequestDispatcher(basePath+"bgIndex.jsp").forward(req,resp);
        }

    }

    //分类网关
    protected void categorylist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("categories",categoryService.queryAll());
        req.getRequestDispatcher(basePath+"category_list.jsp").forward(req,resp);
    }

    //评论审核网关
    protected void bgcomment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("comments",commentService.queryAllS0());
        req.getRequestDispatcher(basePath+"comment_bg.jsp").forward(req,resp);
    }

    //历史记录网关
    protected void historyrecord(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"history_record.jsp").forward(req,resp);
    }
    //消息网关
    protected void message(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"message.jsp").forward(req,resp);
    }
    //个人中心网关
    protected void mine(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        req.setAttribute("user",user);
        req.getRequestDispatcher(basePath+"mine.jsp").forward(req,resp);
    }
    //角色列表网关
    protected void rolelist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("roles",roleService.queryAll());
        req.setAttribute("authorities",authorityService.queryAll());
        req.getRequestDispatcher(basePath+"role_list.jsp").forward(req,resp);
    }
    //搜索网关
    protected void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"searchResult.jsp").forward(req,resp);
    }
    //个人信息网关
    protected void userinfo(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"user_info.jsp").forward(req,resp);
    }
    //完善个人信息网关
    protected void userinfocomplete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"user_info_complete.jsp").forward(req,resp);
    }
    //用户管理网关
    protected void userlist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"user_list.jsp").forward(req,resp);
    }
    //视频列表网关
    protected void videolist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            req.getRequestDispatcher(basePath+"login.jsp").forward(req,resp);
        }
        Video video = new Video();
        video.setUserId(user.getId());
        req.setAttribute("videos", videoService.queryByConditions(video));
        //查找分类
        Map<Long , Category> categoryMap = new HashMap<>();
        List<Category> categories = categoryService.queryAll();
        for (Category category : categories) {
            categoryMap.put(category.getId(),category);
        }
        req.setAttribute("categoryMap",categoryMap);
        req.getRequestDispatcher(basePath+"video_list.jsp").forward(req,resp);
    }
    //视频审核网关
    protected void bgvideolist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("videos", videoService.queryAllS0());
        req.getRequestDispatcher(basePath+"video_list_bg.jsp").forward(req,resp);
    }
    //视频播放网关
    protected void play(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"video_play.jsp").forward(req,resp);
    }
    //视频上传网关
    protected void videoupload(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("categorys",categoryService.queryAll());
        req.getRequestDispatcher(basePath+"video_upload.jsp").forward(req,resp);
    }

    //收藏夹
    protected void favorites(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher(basePath+"favorites.jsp").forward(req,resp);
    }
}
